package cn.kn.triage.sys.service.impl;

import cn.kn.triage.common.model.R;
import cn.kn.triage.common.servic.BaseService;
import cn.kn.triage.sys.dao.PermissionDao;
import cn.kn.triage.sys.dao.RolePermissionDao;
import cn.kn.triage.sys.mapper.RolePermissionMapper;
import cn.kn.triage.sys.model.RolePermissionExample;
import cn.kn.triage.sys.service.PermissionService;
import cn.kn.triage.sys.vo.RolePerm;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * @author yinzn
 * @create 2020-10-18 14:21
 */
@Service
public class PermissionServiceImpl extends BaseService implements PermissionService {
    private final PermissionDao permissionDao;
private final RolePermissionDao rolePermissionDao;
private final RolePermissionMapper rolePermissionMapper;
    public PermissionServiceImpl(PermissionDao permissionDao, RolePermissionDao rolePermissionDao, RolePermissionMapper rolePermissionMapper) {

        this.permissionDao = permissionDao;
        this.rolePermissionDao = rolePermissionDao;
        this.rolePermissionMapper = rolePermissionMapper;
    }

    @Override
    public R right() {
        return R.ok(permissionDao.getRight());
    }

    @Override
    public R getIdsByRoleId(Integer roleId) {
        return R.ok(permissionDao.getIdsByRoleId(roleId));
    }
    @Transactional
    @Override
    public R saveRight(Integer roleId, RolePerm[] rolePerms) {
        RolePermissionExample example = new RolePermissionExample();
        example.createCriteria().andRoleidEqualTo(roleId);
        rolePermissionMapper.deleteByExample(example);
        if (rolePerms!=null && rolePerms.length>0){
            rolePermissionDao.batchInsert(rolePerms);
        }
        return R.ok();
    }


}
